package kr.saeildamil.androidstudy_second33;

import kr.saeildamil.androidstudy_second.R;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Audio;
import android.provider.MediaStore.Images;
import android.provider.MediaStore.MediaColumns;
import android.provider.MediaStore.Video;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.ToggleButton;

public class DumpMedia extends Activity {
	TextView mResult;
	ContentResolver mCr;
	RadioGroup mMedia;
	ToggleButton mStorage;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch33_dumpmedia);

		mCr=getContentResolver();
		mResult=(TextView) findViewById(R.id.result);
		mStorage=(ToggleButton) findViewById(R.id.storage);
		mMedia=(RadioGroup) findViewById(R.id.media);

		mMedia.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				dumpQuery();
			}
		});
		mStorage.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				dumpQuery();
			}
		});
		dumpQuery();
		
		IntentFilter filter=new IntentFilter();
		filter.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED);
		filter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
		filter.addAction(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
		filter.addDataScheme("file");
		registerReceiver(mScanReceiver, filter);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		unregisterReceiver(mScanReceiver);
	}
	
	BroadcastReceiver mScanReceiver=new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			dumpQuery();
		}
	};

	void dumpQuery(){
		StringBuilder result=new StringBuilder();
		Uri uri = null;
		boolean instorage=mStorage.isChecked();

		//미디어 종류와 메모리 위치로부터 URI 결정
		switch (mMedia.getCheckedRadioButtonId()) {
		case R.id.image:
			uri=instorage?Images.Media.INTERNAL_CONTENT_URI:Images.Media.EXTERNAL_CONTENT_URI;
			break;

		case R.id.audio:
			uri=instorage?Audio.Media.INTERNAL_CONTENT_URI:Audio.Media.INTERNAL_CONTENT_URI;
			break;
		case R.id.video:
			uri=instorage?Video.Media.INTERNAL_CONTENT_URI:Video.Media.INTERNAL_CONTENT_URI;
			break;
		}

		Cursor cursor=mCr.query(uri, null, null, null, null);

		//필드 목록 출력
		int nCount=cursor.getColumnCount();
		result.append("num colume = "+nCount+"\n\n");
		for(int i=0; i<nCount; i++){
			result.append(instorage);
			result.append(":"+cursor.getColumnName(i)+"\n");
		}

		result.append("\n=================================================");

		// 레코드 목록 출력
		result.append("num media = " + cursor.getCount() + "\n\n");
		int count = 0;
		while (cursor.moveToNext()) {
			result.append(getColumeValue(cursor, MediaColumns._ID));
			result.append(getColumeValue(cursor, MediaColumns.DISPLAY_NAME));
			result.append(getColumeValue(cursor, MediaColumns.TITLE));
			result.append(getColumeValue(cursor, MediaColumns.SIZE));
			result.append(getColumeValue(cursor, MediaColumns.DATE_ADDED));
			result.append(getColumeValue(cursor, MediaColumns.MIME_TYPE));

			switch (mMedia.getCheckedRadioButtonId()) {
			case R.id.image:
				result.append(getColumeValue(cursor, Images.ImageColumns.DATE_TAKEN));
				result.append(getColumeValue(cursor, Images.ImageColumns.DESCRIPTION));
				result.append(getColumeValue(cursor, Images.ImageColumns.ORIENTATION));
				result.append(getColumeValue(cursor, Images.ImageColumns.LATITUDE));
				break;
			case R.id.audio:
				result.append(getColumeValue(cursor, Audio.AudioColumns.ALBUM));
				result.append(getColumeValue(cursor, Audio.AudioColumns.ARTIST));
				result.append(getColumeValue(cursor, Audio.AudioColumns.YEAR));
				result.append(getColumeValue(cursor, Audio.AudioColumns.DURATION));
				break;
			case R.id.video:
				result.append(getColumeValue(cursor, Video.VideoColumns.DURATION));
				result.append(getColumeValue(cursor, Video.VideoColumns.RESOLUTION));
				break;
			}
			result.append("\n");
			count++;
			if (count == 32) break;
		}
		cursor.close();

		mResult.setText(result.toString());
	}

	String getColumeValue(Cursor cursor, String cname) {
		String value = cname + " : " +
				cursor.getString(cursor.getColumnIndex(cname)) + "\n";
		return value;
	}

}
